﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Protell.DAL;
using Protell.Model;
using Protell.Model.IRepository;
using System.Collections.ObjectModel;

namespace Protell.ViewModel
{

    public class ConsideracionViewModel : ViewModelBase
    {

        // ***************************** ***************************** *****************************
        // Repository.
        private IConsideracion _ConsideracionRepository;

        public CondProModel CondPro
        {
            get { return _CondPro; }
            set
            {
                if (_CondPro != value)
                {
                    _CondPro = value;
                    OnPropertyChanged(CondProPropertyName);
                }
            }
        }
        private CondProModel _CondPro;
        public const string CondProPropertyName = "CondPro";


        // ***************************** ***************************** *****************************
        // Coleccion para extraer los datos para el grid.
        public ObservableCollection<ConsideracionModel> Consideracions
        {
            get { return _Consideracions; }
            set
            {
                if (_Consideracions != value)
                {
                    _Consideracions = value;
                    OnPropertyChanged(ConsideracionsPropertyName);
                }
            }
        }
        private ObservableCollection<ConsideracionModel> _Consideracions;
        public const string ConsideracionsPropertyName = "Consideracions";

        // ***************************************************************************
        // ***************************************************************************
        // RelayCommand DetailCommand

        public RelayCommand DetailCommand
        {
            get
            {
                if (_DetailCommand == null)
                {
                    _DetailCommand = new RelayCommand(p => this.AttemptDetail(), p => this.CanDetail());
                }

                return _DetailCommand;
            }

        }
        private RelayCommand _DetailCommand;

        public bool CanDetail()
        {
            bool _CanDetail = false;

            foreach (ConsideracionModel w in this.Consideracions)
            {
                // (String.IsNullOrEmpty(w.PlantillaDetalleName) )
                // ( w.TipoElemento != null && !String.IsNullOrEmpty(w.TipoElemento.TipoElementoName) )

                if ( (String.IsNullOrEmpty(w.ConsideracionName)))
                {
                    _CanDetail = false;
                    break;
                }
                else
                {
                    _CanDetail = true;
                }

            }

            return _CanDetail;
        }
        public void AttemptDetail()
        {
            this._ConsideracionRepository.InsertConsideracion(this.Consideracions, this.CondPro);
        }


        // ***************************************************************************
        // ***************************************************************************
        // DeleteCommand.

        public RelayCommand DeleteCommand
        {
            get
            {
                if (_DeleteCommand == null)
                {
                    _DeleteCommand = new RelayCommand(p => this.AttemptDelete(), p => this.CanDelete());
                }

                return _DeleteCommand;
            }

        }
        private RelayCommand _DeleteCommand;

        public bool CanDelete()
        {
            bool _CanDelete = false;

            foreach (ConsideracionModel p in this.Consideracions)
            {
                if (p.IsChecked)
                {
                    _CanDelete = true;
                    break;

                }
            }

            return _CanDelete;
        }
        public void AttemptDelete()
        {
            //TODO : Delete to database
            List<ConsideracionModel> delPlan = null;
            try
            {
                delPlan = (from o in this.Consideracions
                           where o.IsChecked == true
                           select o).ToList();
            }
            catch (Exception)
            {
            }

            this._ConsideracionRepository.DeleteConsideracion(delPlan);
            this.LoadInfoGrid();
        }

        // ***************************** ***************************** *****************************
        // Constructor y carga de elementos.
        public ConsideracionViewModel(CondProModel CondPro)
        {
            this._ConsideracionRepository = new Protell.DAL.Repository.ConsideracionRepository();
            this.CondPro = CondPro;
            this.LoadInfoGrid();
        }

        public void LoadInfoGrid()
        {
            this.Consideracions = this._ConsideracionRepository.GetConsideracions(this.CondPro) as ObservableCollection<ConsideracionModel>;
        }
    }
}
